/*
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * This file is part of Harmattan SmashMiner OpenGL game application.
 * 
 * Harmattan SmashMiner OpenGL Example Application version 1.0.0
 *  
 * Latest update: 15.4.2011
 *
 * The Harmattan SmashMiner OpenGL example application demonstrates how to use 
 * the OpenGL ES in Harmattan devices.
 *
 * This example is provided as a starting point for 3rd party
 * developers (you) to ease the implementation of OpenGL based 
 * games. Ideas, parts of code or methodologies that this
 * example application uses can be freely used without restrictions.
 *
 * See file README.txt how to build and compile this example application
 * in the Harmattan SDK environment. 
 * 
 * See file INSTALL.txt to find out what is required to run this
 * application and how to install the application to the device or
 * alternatively in QEMU-emulator.
 *
 * The LICENSE.txt file explains the license this software is provided
 * with. A copy of the same license is included in this source file.
 * 
 */

/*
 * Copyright (C) 2011 by Nokia Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef GAMETIMER_H
#define GAMETIMER_H

#include "defines.h"     // include this before anything else.

#include <QObject>

class GameTimer : public QObject
{
    Q_OBJECT

    friend class TimerManager;

public:

    /*! 
     * \enum TimerType
     * Timer types for identifying GameTimer instances.
     */
    enum TimerType {

        // game-state-related timers.

        /// Timer type for identifying a ''Get Ready'' game state timer (game play starting) 
        stateGetReadyTimer = 0,
        /// Timer type for indentifying a game state timer used when changing the game level
        stateLevelChangingTimer,
        /// Timer type for identifying a timer used when player crashes
        statePlayerCrashedTimer,
        /// Timer type for indentifying a ''Game Over'' game state timer
        stateGameOverTimer,

        // spaceship-related timers.

        /// When ''Ship safe''-timer is on the miner is in protected mode.
        shipSafeTimer,
        /// ''Ball-to-cone''-mode timer is used to animate the ship from ball mode to cone mode
        shipBall2ConeTimer,
        /// ''Cone-to-ball''-mode timer is used to animate the ship from cone mode to ball mode
        shipCone2BallTimer,

        // effect timers.

        /// To identify timer used for hyper jump effects
        hyperJumpOutTimer,
        /// To identify timer used for hyper jump effects
        hyperJumpInTimer,
        /// To identify timer used for bomb effect
        bombEffectTimer,

        // other timers.

        /// To identify timer used for populating game objects to game 
        moreObjectsTimer,

        // Keep this count at the end of timer types:

        /// To keep the count of timer types
        NumTimers
    };

public:

    explicit GameTimer(TimerType type, int endTime, QObject *parent = 0);

    void start(int startTime = 0);
    void stop();

    bool isActive() const;

    bool isFirstQuarterTime();
    bool isHalfTime();
    bool isLastQuarterTime();
    bool isFullTime();

    /*! Returns the current time of the timer 
     * \return Current running time of the timer. 
     */
    inline int time() const { return currentTime; }

    /*! Returns the timer length --- the ending time of the timer.
     * \return Length of the timer
     */
    inline int timerLength() const { return fullTime; }

    /*! Returns the type of the timer
     * \return One of \c GameTimer::TimerType game timer enumeration types
     */
    inline TimerType type() const { return timerType; }

signals:

public slots:

protected:

    void incTime(int timeStep);

private:

    TimerType timerType;

    int currentTime;

    int firstQuarterTime;
    int halfTime;
    int lastQuarterTime;
    int fullTime;

    bool firstQuarterTimeReplied;
    bool halfTimeReplied;
    bool lastQuarterTimeReplied;
    bool fullTimeReplied;
};

class LocationTimer : public GameTimer
{
    Q_OBJECT

public:

    LocationTimer(TimerType, int eTime, QObject *parent = 0);

    inline FPtype posX() { return positionX; }
    inline FPtype posY() { return positionY; }

    inline void setPosX(FPtype posX) { positionX = posX; }
    inline void setPosy(FPtype posY) { positionY = posY; }

private:

    FPtype positionX;
    FPtype positionY;

};

class BombTimer : public LocationTimer
{
    Q_OBJECT

public:

    BombTimer(TimerType, int eTime, QObject *parent = 0);

    inline FPtype shockRadius() { return -20.0 + (FPtype) time() * 350.0 / (FPtype) timerLength(); }
    inline FPtype lightRadius() { return -40.0 + (FPtype) time() * 325.0 / (FPtype) timerLength(); }
};

#endif // GAMETIMER_H
